//
//  SDAnswerTestSubjectList.m
//
//  Created by   on 2019/3/11
//  Copyright (c) 2019 __MyCompanyName__. All rights reserved.
//

#import "SDAnswerTestSubjectList.h"
#import "SDAnswerTestOptionList.h"


NSString *const kSDAnswerTestSubjectListDesc = @"desc";
NSString *const kSDAnswerTestSubjectListOptionList = @"optionList";


@interface SDAnswerTestSubjectList ()

- (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict;

@end

@implementation SDAnswerTestSubjectList

@synthesize desc = _desc;
@synthesize optionList = _optionList;


+ (instancetype)modelObjectWithDictionary:(NSDictionary *)dict {
    return [[self alloc] initWithDictionary:dict];
}

- (instancetype)initWithDictionary:(NSDictionary *)dict {
    self = [super init];
    
    // This check serves to make sure that a non-NSDictionary object
    // passed into the model class doesn't break the parsing.
    if (self && [dict isKindOfClass:[NSDictionary class]]) {
            self.desc = [self objectOrNilForKey:kSDAnswerTestSubjectListDesc fromDictionary:dict];
    NSObject *receivedSDAnswerTestOptionList = [dict objectForKey:kSDAnswerTestSubjectListOptionList];
    NSMutableArray *parsedSDAnswerTestOptionList = [NSMutableArray array];
    
    if ([receivedSDAnswerTestOptionList isKindOfClass:[NSArray class]]) {
        for (NSDictionary *item in (NSArray *)receivedSDAnswerTestOptionList) {
            if ([item isKindOfClass:[NSDictionary class]]) {
                [parsedSDAnswerTestOptionList addObject:[SDAnswerTestOptionList modelObjectWithDictionary:item]];
            }
       }
    } else if ([receivedSDAnswerTestOptionList isKindOfClass:[NSDictionary class]]) {
       [parsedSDAnswerTestOptionList addObject:[SDAnswerTestOptionList modelObjectWithDictionary:(NSDictionary *)receivedSDAnswerTestOptionList]];
    }

    self.optionList = [NSArray arrayWithArray:parsedSDAnswerTestOptionList];

    }
    
    return self;
    
}

- (NSDictionary *)dictionaryRepresentation {
    NSMutableDictionary *mutableDict = [NSMutableDictionary dictionary];
    [mutableDict setValue:self.desc forKey:kSDAnswerTestSubjectListDesc];
    NSMutableArray *tempArrayForOptionList = [NSMutableArray array];
    
    for (NSObject *subArrayObject in self.optionList) {
        if ([subArrayObject respondsToSelector:@selector(dictionaryRepresentation)]) {
            // This class is a model object
            [tempArrayForOptionList addObject:[subArrayObject performSelector:@selector(dictionaryRepresentation)]];
        } else {
            // Generic object
            [tempArrayForOptionList addObject:subArrayObject];
        }
    }
    [mutableDict setValue:[NSArray arrayWithArray:tempArrayForOptionList] forKey:kSDAnswerTestSubjectListOptionList];

    return [NSDictionary dictionaryWithDictionary:mutableDict];
}

- (NSString *)description  {
    return [NSString stringWithFormat:@"%@", [self dictionaryRepresentation]];
}

#pragma mark - Helper Method
- (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict {
    id object = [dict objectForKey:aKey];
    return [object isEqual:[NSNull null]] ? nil : object;
}


#pragma mark - NSCoding Methods

- (id)initWithCoder:(NSCoder *)aDecoder {
    self = [super init];

    self.desc = [aDecoder decodeObjectForKey:kSDAnswerTestSubjectListDesc];
    self.optionList = [aDecoder decodeObjectForKey:kSDAnswerTestSubjectListOptionList];
    return self;
}

- (void)encodeWithCoder:(NSCoder *)aCoder
{

    [aCoder encodeObject:_desc forKey:kSDAnswerTestSubjectListDesc];
    [aCoder encodeObject:_optionList forKey:kSDAnswerTestSubjectListOptionList];
}

- (id)copyWithZone:(NSZone *)zone {
    SDAnswerTestSubjectList *copy = [[SDAnswerTestSubjectList alloc] init];
    
    
    
    if (copy) {

        copy.desc = [self.desc copyWithZone:zone];
        copy.optionList = [self.optionList copyWithZone:zone];
    }
    
    return copy;
}


@end
